package com.sh.business.service.impl;

import com.sh.common.core.utils.MapstructUtils;
import com.sh.common.core.utils.StringUtils;
import com.sh.common.mybatis.core.page.TableDataInfo;
import com.sh.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sh.business.domain.bo.MiningBo;
import com.sh.business.domain.vo.MiningVo;
import com.sh.business.domain.Mining;
import com.sh.business.mapper.MiningMapper;
import com.sh.business.service.IMiningService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 矿机Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-08
 */
@RequiredArgsConstructor
@Service
public class MiningServiceImpl implements IMiningService {

    private final MiningMapper baseMapper;

    /**
     * 查询矿机
     *
     * @param id 主键
     * @return 矿机
     */
    @Override
    public MiningVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询矿机列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 矿机分页列表
     */
    @Override
    public TableDataInfo<MiningVo> queryPageList(MiningBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Mining> lqw = buildQueryWrapper(bo);
        Page<MiningVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的矿机列表
     *
     * @param bo 查询条件
     * @return 矿机列表
     */
    @Override
    public List<MiningVo> queryList(MiningBo bo) {
        LambdaQueryWrapper<Mining> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc(Mining::getPrice);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Mining> buildQueryWrapper(MiningBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Mining> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Mining::getName, bo.getName());
        lqw.eq(bo.getPrice() != null, Mining::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), Mining::getIcon, bo.getIcon());
        lqw.eq(bo.getComputility() != null, Mining::getComputility, bo.getComputility());
        lqw.eq(StringUtils.isNotBlank(bo.getTerm()), Mining::getTerm, bo.getTerm());
        lqw.eq(StringUtils.isNotBlank(bo.getDailyCapacity()), Mining::getDailyCapacity, bo.getDailyCapacity());
        return lqw;
    }

    /**
     * 新增矿机
     *
     * @param bo 矿机
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(MiningBo bo) {
        Mining add = MapstructUtils.convert(bo, Mining.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改矿机
     *
     * @param bo 矿机
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(MiningBo bo) {
        Mining update = MapstructUtils.convert(bo, Mining.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Mining entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除矿机信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
